package com.geor.gcv.core.process;

import org.opencv.core.Mat;

/**
 * 调整对比处理
 * 
 * @author geor.lusongnan
 *
 */
public class ContrastProcess extends Process {
	private Process process;
	private double alpha = 1;

	public ContrastProcess(Process process, double alpha) {
		super();
		this.process = process;
		this.alpha = alpha;
	}

	@Override
	public String getProcessName() {
		return process.getProcessName() + "-->调整对比度";
	}

	@Override
	public Mat process() {
		Mat img = process.process();
		Mat result = new Mat(img.size(), img.type());
		if (alpha == 1) {
			return img.clone();
		}
		if (img.channels() == 3) {
			for (int row = 0; row < img.rows(); row++) {
				for (int col = 0; col < img.cols(); col++) {
					double[] pex = img.get(row, col);
					double[] temp = new double[3];
					for (int i = 0; i < pex.length; i++) {
						temp[i] = getValue(pex[i]);
					}
					result.put(row, col, temp);
				}
			}
			return result;
		} else if (img.channels() == 1) {
			for (int row = 0; row < img.rows(); row++) {
				for (int col = 0; col < img.cols(); col++) {
					double pex = img.get(row, col)[0];
					result.put(row, col, getValue(pex));
				}
			}
			return result;
		}
		return img;
	}

	private double getValue(double value) {
		return value * alpha >= 255 ? 255 : value;
	}

}
